/**
 * 定义项目名称
 */
import i18n from '../components/i18n/i18n.js';


const W_PROJECT_NAME = '';

const isTest = true;

let util = {

};
util.title = function(title) {
	title = title ? title + ' - AllSale' : 'wsplus Project';
	window.document.title = title;
};

/**
 * 判断是否为DEV模式
 */
util.isDev = function() {
	return process.env.NODE_ENV === 'development';
};

/**
 * 返回手机版预览域名
 */
util.previewUrl = function() {
	return util.isDev() ? 'http://wstro.eicp.net:93' : 'https://app.hzhljt.com'
};

/**
 * 返回项目名称/ops
 */
util.projectName = function() {
	return util.isDev() ? '' : (W_PROJECT_NAME == '' ? '' : '/' + W_PROJECT_NAME);
};


// 取出存入localStorage中的语言

let thisCurrentLanguage=localStorage.getItem('currentLanguage');

console.log(thisCurrentLanguage);

console.log(i18n.messages);

util.name = function() {
	// return 'AllSale商户系统';
	if(thisCurrentLanguage=='zh'){
		return 'AllSale商户系统';
	}else if(thisCurrentLanguage=='en'){
		return 'AllSale merchant system';		
	}else if(thisCurrentLanguage=='it'){
		return 'Sistema mercantile AllSale'
	}
	
}

/**
 * 返回登录页面
 */
util.loginPath = function() {
	return util.projectName() + '/login.html';
};
/**
 * 返回首页
 */
util.homePath = function() {
	return util.projectName() + '/index';
};

/**
 * 跳转到商户信息页面
 */
util.merchantPath = function() {
	return util.projectName() + '/merchant';
};


util.profilePath = function() {
	return util.projectName() + '/profile';
};

/**
 * 获取TOKEN KEY
 */
util.tokenKey = function() {
	return  "allsale_sh_auth_tokenId";
};

/**
 * 路由名称
 */
util.routeName = {
	home: 'sys_index', //系统首页
	profile: 'sys_account_profile' //账户设置
};

/**
 * 腾讯上传服务
 */
util.cos = {
	// Bucket: 'allsale-dev-1256326697',
	// Region: 'ap-guangzhou'
	Bucket: 'allsale-it-1256326697',
	Region: 'eu-frankfurt'
};

util.vad = {
	/**
	 * 判断是否为空
	 * @param {Object} value
	 */
	isBlank: function(value) {
		return value === undefined || null === value || '' === value;
	},
	//数字校验(小数,0,负数都可以)
	validateNum: function(rule, value, callback) {
		if(!value) {
			return callback();
		}
		setTimeout(() => {
			var regUrl = /^(-?\d+)(\.\d+)?$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入数字'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter the number'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire un numero'));
				}
				
			} else {
				callback();
			}
		}, 100);
	},
	//自然数校验(大于等于0)
	validateNaturalNumber: function(rule, value, callback) {
		if(!value) {
			return callback();
		};
		setTimeout(() => {
			var regUrl = /^[0-9]*$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入自然数'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter a natural number'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire un numero naturale'));
				}
				
			} else {
				callback();
			}
		}, 100);
	},
	//正整数校验(不能为0)
	validatePositiveInteger: function(rule, value, callback) {
		if(!value) {
			return callback();
		};
		setTimeout(() => {
			var regUrl = /^[0-9]*[1-9][0-9]*$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入正整数'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter a positive integer'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Per favore inserisci un numero intero positivo'));
				}
				
			} else {
				callback();
			}
		}, 100);
	},
	//手机号校验
	validatePhoneNumber: function(rule, value, callback) {
		if(!value) {
			if(thisCurrentLanguage=='zh'){
				return callback(new Error('手机号码不能为空'));
			}else if(thisCurrentLanguage=='en'){
				return callback(new Error('Phone number can not be blank'));
			}else if(thisCurrentLanguage=='it'){
				return callback(new Error('Il numero di cellulare non può essere vuoto'));
			}
			
		};
		setTimeout(() => {
			var regUrl = /^1(3|4|5|7|8)\d{9}$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('手机号码格式不正确'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Phone number format is incorrect'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Il formato del numero di telefono non è corretto'));
				}
				
			} else {
				callback();
			}
		}, 100);
	},
	//网址校验
	validateUrl: function(rule, value, callback) {
		if(WUtil.vad.isBlank(value)) {
			if(thisCurrentLanguage=='zh'){
				callback(new Error('请输入链接地址'));
			}else if(thisCurrentLanguage=='en'){
				callback(new Error('Please enter the link address'));
			}else if(thisCurrentLanguage=='it'){
				callback(new Error("Si prega di inserire l'indirizzo del link"));
			}
			
		};
		setTimeout(() => {
			var regUrl = /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)*(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('网址格式不正确,例：http://www.xxxx.com'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('The URL format is incorrect, for example: http://www.xxxx.com'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error("Il formato dell'URL non è corretto, ad esempio: http://www.xxxx.com"));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//电话校验
	validateTelephone: function(rule, value, callback) {
		if(WUtil.vad.isBlank(value)) {
			callback();
		};
		setTimeout(() => {
			var regPhone = /^0\d{2,3}-?\d{7,8}$/;
			if(!regPhone.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入正确的电话号码'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter the correct phone number'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire il numero di telefono corretto'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//电话或手机校验
	validateTelephoneNumber: function(rule, value, callback) {
		if(WUtil.vad.isBlank(value)) {
			callback();
		};
		setTimeout(() => {
			var regPhone = /(^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}?$)|(^((\(\d{3}\))|(\d{3}\-))?(1[34578]\d{9})$)/;
			if(!regPhone.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入正确的联系方式'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter the correct contact method'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire il metodo di contatto corretto'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//特殊符号校验
	validateSpecialCharacters: function(rule, value, callback) {
		if(!value) {
			return callback();
		}
		setTimeout(() => {
			var containSpecial = RegExp(/[(\ )(\~)(\!)(\@)(\#)(\$)(\%)(\^)(\&)(\*)(\()(\))(\-)(\_)(\+)(\=)(\[)(\])(\{)(\})(\|)(\\)(\;)(\:)(\')(\")(\,)(\.)(\/)(\<)(\>)(\?)(\)]+/);
			if(containSpecial.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('内容不能含有特殊字符'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Content cannot contain special characters'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Il contenuto non può contenere caratteri speciali'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//密码校验
	validatePassword: function(rule, value, callback) {
		if(!value) {
			return callback();
		}
		setTimeout(() => {
			var regUrl = /^[a-zA-Z][a-zA-Z0-9_]*$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('以字母开头，长度在6-18之间，只能包含字母、数字和下划线'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Start with a letter and be between 6 and 18 in length. It can only contain letters, numbers and underscores.'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Inizia con una lettera e dai 6 ai 18 di lunghezza e può contenere solo lettere, numeri e caratteri di sottolineatura.'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//邮箱校验
	validateEmail: function(rule, value, callback) {
		if(!value) {
			return callback();
		}
		setTimeout(() => {
			var regUrl = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入正确格式的邮箱号码'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter the correct format of the mailbox number'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire il formato corretto del numero di casella postale'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//身份证校验
	validateIdCard: function(rule, value, callback) {
		if(!value) {
			return callback();
		}
		setTimeout(() => {
			var regUrl = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入正确格式的身份证号码'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter the correct format ID number'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire il numero ID di formato corretto'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//大于0小于1的小数
	validateDecimal: function(rule, value, callback) {
		if(!value) {
			return callback();
		}
		setTimeout(() => {
			var regUrl = /^0\.[1-9]\d*$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入0到1的小数'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter a decimal from 0 to 1.'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire un decimale da 0 a 1.'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//百分比校验
	validatePercentage: function(rule, value, callback) {
		if(!value) {
			return callback();
		}
		setTimeout(() => {
			var regUrl = /^(100|[1-9]?\d(\.\d\d?)?)%$/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入正确的百分比格式'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter the correct percentage format'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Si prega di inserire il formato percentuale corretto'));
				}
				
			} else {
				callback();
			}
		}, 200);
	},
	//价格校验
	validatePrice: function(rule, value, callback) {
		if(!value) {
			if(thisCurrentLanguage=='zh'){
				callback(new Error('不能为空'));
			}else if(thisCurrentLanguage=='en'){
				callback(new Error('Can not be empty'));
			}else if(thisCurrentLanguage=='it'){
				callback(new Error('Non può essere vuoto'));
			}
			
		}
		setTimeout(() => {
			var regUrl = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
			if(!regUrl.test(value)) {
				if(thisCurrentLanguage=='zh'){
					callback(new Error('请输入正确格式的价格(x.xx;x.x;x)'));
				}else if(thisCurrentLanguage=='en'){
					callback(new Error('Please enter the price in the correct format (x.xx;x.x;x)'));
				}else if(thisCurrentLanguage=='it'){
					callback(new Error('Inserisci il prezzo nel formato corretto (x.xx; x.x; x)'));
				}
				
			} else {
				callback();
			}
		}, 100);
	}
};

/**
 * 日期函数
 */
util.date = {
	strToDate: function(str) {
		var fullDate = str.split('-');
		return new Date(fullDate[0], fullDate[1] - 1, fullDate[2], 0, 0, 0);
	},
	formatDate: function(date, format) {
		var v = "";
		if(typeof date == "string" || typeof date != "object") {
			return;
		}
		var year = date.getFullYear();
		var month = date.getMonth() + 1;
		var day = date.getDate();
		var hour = date.getHours();
		var minute = date.getMinutes();
		var second = date.getSeconds();
		var weekDay = date.getDay();
		var ms = date.getMilliseconds();
		var weekDayString = "";

		if(thisCurrentLanguage=='zh'){
			if(weekDay == 1) {
				weekDayString = "星期一";
			} else if(weekDay == 2) {
				weekDayString = "星期二";
			} else if(weekDay == 3) {
				weekDayString = "星期三";
			} else if(weekDay == 4) {
				weekDayString = "星期四";
			} else if(weekDay == 5) {
				weekDayString = "星期五";
			} else if(weekDay == 6) {
				weekDayString = "星期六";
			} else if(weekDay == 7) {
				weekDayString = "星期日";
			}
		}else if(thisCurrentLanguage=='en'){
			if(weekDay == 1) {
				weekDayString = "Monday";
			} else if(weekDay == 2) {
				weekDayString = "Tuesday";
			} else if(weekDay == 3) {
				weekDayString = "Wednesday";
			} else if(weekDay == 4) {
				weekDayString = "Thursday";
			} else if(weekDay == 5) {
				weekDayString = "Friday";
			} else if(weekDay == 6) {
				weekDayString = "Saturday";
			} else if(weekDay == 7) {
				weekDayString = "Sunday";
			}
		}else if(thisCurrentLanguage=='it'){
			if(weekDay == 1) {
				weekDayString = "lunedi";
			} else if(weekDay == 2) {
				weekDayString = "martedì";
			} else if(weekDay == 3) {
				weekDayString = "mercoledì";
			} else if(weekDay == 4) {
				weekDayString = "giovedi";
			} else if(weekDay == 5) {
				weekDayString = "venerdì";
			} else if(weekDay == 6) {
				weekDayString = "sabato";
			} else if(weekDay == 7) {
				weekDayString = "domenica";
			}
		}	

		

		v = format;
		//Year 
		v = v.replace(/yyyy/g, year);
		v = v.replace(/YYYY/g, year);
		v = v.replace(/yy/g, (year + "").substring(2, 4));
		v = v.replace(/YY/g, (year + "").substring(2, 4));

		//Month 
		var monthStr = ("0" + month);
		v = v.replace(/MM/g, monthStr.substring(monthStr.length - 2));

		//Day 
		var dayStr = ("0" + day);
		v = v.replace(/dd/g, dayStr.substring(dayStr.length - 2));

		//hour 
		var hourStr = ("0" + hour);
		v = v.replace(/HH/g, hourStr.substring(hourStr.length - 2));
		v = v.replace(/hh/g, hourStr.substring(hourStr.length - 2));

		//minute 
		var minuteStr = ("0" + minute);
		v = v.replace(/mm/g, minuteStr.substring(minuteStr.length - 2));

		//Millisecond 
		v = v.replace(/sss/g, ms);
		v = v.replace(/SSS/g, ms);

		//second 
		var secondStr = ("0" + second);
		v = v.replace(/ss/g, secondStr.substring(secondStr.length - 2));
		v = v.replace(/SS/g, secondStr.substring(secondStr.length - 2));

		//weekDay 
		v = v.replace(/E/g, weekDayString);
		return v;
	},
	/*
	 * 获取上一个月
	 */
	getPreMonth: function(date) {
		var arr = date.split('-');
		var year = arr[0]; //获取当前日期的年份
		var month = arr[1]; //获取当前日期的月份
		var day = arr[2]; //获取当前日期的日
		var days = new Date(year, month, 0);
		days = days.getDate(); //获取当前日期中月的天数
		var year2 = year;
		var month2 = parseInt(month) - 1;
		if(month2 == 0) {
			year2 = parseInt(year2) - 1;
			month2 = 12;
		}
		var day2 = day;
		var days2 = new Date(year2, month2, 0);
		days2 = days2.getDate();
		if(day2 > days2) {
			day2 = days2;
		}
		if(month2 < 10) {
			month2 = '0' + month2;
		}
		var t2 = year2 + '-' + month2 + '-' + day2;
		return t2;
	},
	chsDay: ['日', '一', '二', '三', '四', '五', '六'],
};

//默认地址坐标
util.mapCoordinate = {
	lng: 116.404,
	lat: 39.915,
	address: '北京市东城区'
};

util.uuid = function() {
	var s = [];
	var hexDigits = "0123456789abcdef";
	for(var i = 0; i < 36; i++) {
		s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
	}
	s[14] = "4"; // bits 12-15 of the time_hi_and_version field to 0010
	s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
	s[8] = s[13] = s[18] = s[23] = "-";

	var uuid = s.join("");
	return uuid;
};

/**
 * 获取文件后缀
 * @param {Object} fileName
 */
util.getFileSuffix = function(fileName) {
	var index1 = fileName.lastIndexOf(".");
	var index2 = fileName.length;
	var suffix = fileName.substring(index1 + 1, index2);
	return suffix;
}

/**
 * 字符串截取
 */
if(!String.prototype.trim) {

	/*---------------------------------------
	 * 清除字符串两端空格，包含换行符、制表符
	 *---------------------------------------*/
	String.prototype.trim = function() {
		return this.triml().trimr();
	}

	/*----------------------------------------
	 * 清除字符串左侧空格，包含换行符、制表符
	 * ---------------------------------------*/
	String.prototype.triml = function() {
		return this.replace(/^[\s\n\t]+/g, "");
	}

	/*----------------------------------------
	 * 清除字符串右侧空格，包含换行符、制表符
	 *----------------------------------------*/
	String.prototype.trimr = function() {
		return this.replace(/[\s\n\t]+$/g, "");
	}
}

export default util;